			cpu					5307
			
CR				equ		$0D
LF				equ		$0A

in_string
				move.l	D1,-(A7)
				move.l	D2,-(A7)
				move.l	A1,-(A7)
				move.l	#0,D2		; in D2 keep a count of how many characters we have
in_string_loop	jsr 	in_char
				tst.l	D1
				beq		in_string_loop ; ignore null characters if we get them		
				cmp.l	#CR,D1		
				beq.w	exit_in_string		
				cmp.l	#LF,D1	; Note LF gets caught on CF-server when prog run!		
				beq.w	exit_in_string		
				move.b	D1,(A1)+		
				add.l	#1,D2		
				bra		in_string_loop		
exit_in_string	tst.l	D2		
				beq		in_string_loop ; loop back if we have 0 characters		
				move.b 	#0,(A1)		
				move.l	(A7)+,A1
				move.l	(A7)+,D2
				move.l	(A7)+,D1
				rts
out_crlf
				move.l	D1,-(A7)
				move	#CR,D1
				jsr		out_char
				move	#LF,D1
				jsr		out_char
				move.l	(A7)+,D1
				rts

out_string
				move.l	D1,-(A7)
				move.l	A1,-(A7)
out_next_char
				move.b	(A1)+,D1
				jsr		out_char
				tst.b	D1
				bne		out_next_char
				move.l	(A7)+,A1
				move.l	(A7)+,D1
				rts

out_char
				move.l	D0,-(A7)
				move.l	#$0013,d0	;Select function
				TRAP	#15
				move.l	(A7)+,D0
				rts

in_char			move.l	D0,-(A7)
				move.l	#$0010,d0
				TRAP	#15
				and.l	#$7F,d1
				bsr		out_char	; Echo the character back to the user
				move.l	(A7)+,D0
				rts

dec2bin			move.l	D1,-(A7)
				move.l	D2,-(A7)
				move.l	A1,-(A7)	; store location of beginning of buffer
				clr.l 	D0 			; clear d0 (accumulator)
				move.l 	#4,D1 		; set counter to 4
d2bloop			move.b 	(A1)+,D2 	; move character to register
				sub.l 	#$30,D2 	; convert to dec number
				mulu.w 	#10,D0 		; multiply accumalator by 10
				add.l 	D2,D0 		; add dec number to accumalator
				sub.l 	#1,D1 		; decrement counter
				bne 	d2bloop		; check if 4 digits have been parsed
				move.l	(A7)+,A1	; point to beginning of buffer again
				move.l	(A7)+,D2
				move.l	(A7)+,D1
				rts					; return from subroutine

bin2dec			clr.l d4 				; counter
				move.w d1,d2 			; fetch word-sized binary number from d1 and store a copy in d2 (d1 is not modified)
				move.l #4,d4 			; set counter to 4
	
b2dloopa		divu.w #10,d2 			; divide 16-bit input by 10 (16r:16q) and store in d2
				move.w d2,d3 			; copy lower 16 bits (quotient) to d3 before performing operations
				lsr.l #8,d2 			; shift right 16 bits (8 bits twice) to get the remainder in upper 16 bits of d2
				lsr.l #8,d2 
				add.l #$30,d2 			; add 0x30 to the remainder to convert it to ascii
				move.w d2,(a2)+ 		; move ascii character to a2 and increment a2
				move.w d3,d2 			; copy current quotient value to d2 for next cycle
				sub.l #1,d4 			; decrement counter
				bne b2dloopa			; check if 4 digits have been parsed
	
				move.l #4,d4 			; set counter to 4
b2dloopb		move.b -(a2),-(a7) 		; pop current digit from reversed string and push onto stack
				sub.l #1,d4 			; decrement counter
				bne b2dloopb 			; check if 4 digits have been parsed
				
				move.l #4,d4 			; set counter to 4
b2dloopc		move.b (a7)+,(a2)+ 		; pop current digit at top of stack and push into a2 and onwards
				sub.l #1,d4 			; decrement counter
				bne b2dloopc			; check if 4 digits have been parsed
				
				move.b #0,(a2) 			; add null terminator to string							
				rts						; return from subroutine
